// the entire manufacturing.dm

/datum/cook
	var/name = null
	var/item = null
	var/cost1 = null
	var/cost2 = null
	var/cost3 = null
	var/cname1 = null
	var/cname2 = null
	var/cname3 = null
	var/amount1 = 0
	var/amount2 = 0
	var/amount3 = 0
	var/create = 1
	var/time = 5

/obj/machinery/foodicator
	name = "Oven"
	desc = "An oven that can store ingredients into a microsized Bluespace pocket and rapidly cook and produce food from it."
	icon = 'kitchen.dmi'
	var/state = "oven"
	icon_state = "oven"
	density = 1
	anchored = 1
	//mats = 25
	var/working = 0
	var/powconsumption = 0
	var/hacked = 0
	var/acceptdisk = 0
	var/malfunction = 0
	var/electrified = 0
	var/dl_list = null
	var/list/available = list()
	var/list/hidden = list()
	var/wires = 15
	var/const
		WIRE_EXTEND = 1
		WIRE_DISK = 2
		WIRE_MALF = 3
		WIRE_SHOCK = 4

	New()
		..()

	process()
		..()
		if (src.working) use_power(src.powconsumption)
		if(src.electrified > 0) src.electrified--

	ex_act(severity)
		switch(severity)
			if(1.0) del(src)
			if(2.0)
				if (prob(60)) del(src)
			if(3.0)
				if (prob(30)) del(src)
		return

	blob_act()
		if (prob(25)) del src
		return

	power_change()
		if(stat & BROKEN) del(src)
		else
			if( powered() )
				if (src.working) src.icon_state = "[state]-active"
				else src.icon_state = "[state]-idle"
				stat &= ~NOPOWER
			else
				spawn(rand(0, 15))
					src.icon_state = "[state]-off"
					stat |= NOPOWER

	attack_hand(var/mob/user as mob)
		if(stat & BROKEN) return
		if(stat & NOPOWER) return

		user.machine = src
		var/dat = "<B>[src.name]</B><BR><HR>"

		if(src.working)
			dat += "This unit is currently busy."
			user << browse(dat, "window=manufact;size=400x500")
			onclose(user, "manufact")
			return

		var/AMTmeat = 0
		var/AMTwhet = 0
		var/AMTtoma = 0
		var/AMTmshm = 0
		var/AMTbana = 0
		var/AMTchli = 0
		var/AMToran = 0
		var/AMTlett = 0
		var/AMTches = 0
		var/AMTpota = 0
		var/AMTeggs = 0

		for(var/obj/item/weapon/ore/O in src.contents)
			if (istype(O,/obj/item/weapon/reagent_containers/food/snacks/ingredient/meat)) AMTmeat++
			if (istype(O,/obj/item/weapon/reagent_containers/food/snacks/ingredient/flour)) AMTwhet++
			if (istype(O,/obj/item/weapon/reagent_containers/food/snacks/plant/tomato)) AMTtoma++
			if (istype(O,/obj/item/weapon/reagent_containers/food/snacks/mushroom)) AMTmshm++
			if (istype(O,/obj/item/weapon/reagent_containers/food/snacks/plant/banana)) AMTbana++
			if (istype(O,/obj/item/weapon/reagent_containers/food/snacks/plant/chili)) AMTchli++
			if (istype(O,/obj/item/weapon/reagent_containers/food/snacks/plant/orange)) AMToran++
			if (istype(O,/obj/item/weapon/reagent_containers/food/snacks/plant/lettuce)) AMTlett++
			if (istype(O,/obj/item/weapon/reagent_containers/food/snacks/ingredient/cheese)) AMTches++
			if (istype(O,/obj/item/weapon/reagent_containers/food/snacks/plant/potato)) AMTpota++
			if (istype(O,/obj/item/weapon/reagent_containers/food/snacks/ingredient/egg)) AMTeggs++

		dat += "<B>Available Ingredients:</B><BR>"
		if (AMTmeat) dat += "<A href='?src=\ref[src];eject=1'><B>Meat:</B></A> [AMTmeat]<br>"
		if (AMTwhet) dat += "<A href='?src=\ref[src];eject=2'><B>Wheat:</B></A> [AMTwhet]<br>"
		if (AMTtoma) dat += "<A href='?src=\ref[src];eject=3'><B>Tomato:</B></A> [AMTtoma]<br>"
		if (AMTmshm) dat += "<A href='?src=\ref[src];eject=4'><B>Mushroom:</B></A> [AMTmshm]<br>"
		if (AMTbana) dat += "<A href='?src=\ref[src];eject=5'><B>Banana:</B></A> [AMTbana]<br>"
		if (AMTchli) dat += "<A href='?src=\ref[src];eject=6'><B>Chili Pepper:</B></A> [AMTchli]<br>"
		if (AMToran) dat += "<A href='?src=\ref[src];eject=7'><B>Orange:</B></A> [AMToran]<br>"
		if (AMTlett) dat += "<A href='?src=\ref[src];eject=8'><B>Lettuce:</B></A> [AMTlett]<br>"
		if (AMTches) dat += "<A href='?src=\ref[src];eject=9'><B>Cheese:</B></A> [AMTches]<br>"
		if (AMTpota) dat += "<A href='?src=\ref[src];eject=10'><B>Potato:</B></A> [AMTpota]<br>"
		if (AMTeggs) dat += "<A href='?src=\ref[src];eject=11'><B>Egg:</B></A> [AMTeggs]<br>"
		if (!AMTmeat && !AMTwhet && !AMTtoma && !AMTmshm && !AMTbana && !AMTchli && !AMToran && !AMTlett && !AMTches && !AMTpota && !AMTeggs)
			dat += "No ingredients currently loaded.<br>"

		dat += {"<HR>
		<B>Available Schematics</B>"}

		for(var/datum/cook/A in src.available)
			dat += {"<BR><A href='?src=\ref[src];disp=\ref[A]'>
			<b><u>[A.name]</u></b></A><br>
			<b>Cost:</b> [A.amount1] [A.cname1]"}
			if (A.cost2) dat += ", [A.amount2] [A.cname2]"
			if (A.cost3) dat += ", [A.amount3] [A.cname3]"
			dat += "<br><b>Time:</b> [A.time] Seconds<br>"

		if (src.hacked)
			for(var/datum/cook/A in src.hidden)
				dat += {"<BR><A href='?src=\ref[src];disp=\ref[A]'>
				<b><u>[A.name]</u></b></A> (Secret)<br>
				<b>Cost:</b> [A.amount1] [A.cname1]"}
				if (A.cost2) dat += ", [A.amount2] [A.cname2]"
				if (A.cost3) dat += ", [A.amount3] [A.cname3]"
				dat += "<br><b>Time:</b> [A.time] Seconds<br>"

		user << browse(dat, "window=manufact;size=400x500")
		onclose(user, "manufact")

	Topic(href, href_list)
		if(stat & BROKEN) return
		if(stat & NOPOWER) return
		if(usr.stat || usr.restrained())
			return

		if ((usr.contents.Find(src) || ((get_dist(src, usr) <= 1) && istype(src.loc, /turf))))
			usr.machine = src

			if (href_list["eject"])
				var/operation = text2num(href_list["eject"])
				var/ejectamt = 0
				var/ejecting = null
				switch(operation)
					if(1) ejecting = /obj/item/weapon/reagent_containers/food/snacks/ingredient/meat
					if(2) ejecting = /obj/item/weapon/reagent_containers/food/snacks/ingredient/flour
					if(3) ejecting = /obj/item/weapon/reagent_containers/food/snacks/plant/tomato
					if(4) ejecting = /obj/item/weapon/reagent_containers/food/snacks/mushroom
					if(5) ejecting = /obj/item/weapon/reagent_containers/food/snacks/plant/banana
					if(6) ejecting = /obj/item/weapon/reagent_containers/food/snacks/plant/chili
					if(7) ejecting = /obj/item/weapon/reagent_containers/food/snacks/plant/orange
					if(8) ejecting = /obj/item/weapon/reagent_containers/food/snacks/plant/lettuce
					if(9) ejecting = /obj/item/weapon/reagent_containers/food/snacks/ingredient/cheese
					if(10) ejecting = /obj/item/weapon/reagent_containers/food/snacks/plant/potato
					if(11) ejecting = /obj/item/weapon/reagent_containers/food/snacks/ingredient/egg
					else
						usr << "\red Error. Unknown ingredient type."
						return
				sleep(3)
				ejectamt = input(usr,"How many units do you want to eject?","Eject Materials") as num
				for(var/obj/item/weapon/ore/O in src.contents)
					if (ejectamt <= 0) break
					if (istype(O, ejecting))
						O.loc = usr.loc
						ejectamt--

			if (href_list["disp"])
				var/datum/cook/I = locate(href_list["disp"])
				// Material Check
				var/A1 = 0
				var/A2 = 0
				var/A3 = 0
				for(var/obj/item/weapon/ore/O in src.contents)
					if (istype(O,I.cost1)) A1++
					if (istype(O,I.cost2)) A2++
					if (istype(O,I.cost3)) A3++
				if (A1 < I.amount1 || A2 < I.amount2 || A3 < I.amount3)
					usr << "\red Insufficient materials to manufacture that item."
					return
				// Consume Mats
				var/C1 = I.amount1
				var/C2 = I.amount2
				var/C3 = I.amount3
				for(var/obj/item/weapon/reagent_containers/food/O in src.contents)
					if (istype(O,I.cost1) && C1)
						del O
						C1--
					if (istype(O,I.cost2) && C2)
						del O
						C2--
					if (istype(O,I.cost3) && C3)
						del O
						C3--
				// Manufacture Item
				src.icon_state = "[state]-active"
				src.working = 1
				var/worktime = I.time * 10
				var/powconsume = 1500
				/*for(var/i = robotics_research.starting_tier, i <= robotics_research.max_tiers, i++)
					for(var/datum/roboresearch/a in robotics_research.researched_items[i])
						if (a.manubonus)
							worktime -= a.timebonus
							if (a.multiplier != 0) worktime /= a.multiplier
							powconsume -= a.powbonus*/
				if (worktime < 1) worktime = 1
				src.powconsumption = powconsume
				src.updateUsrDialog()
				sleep(worktime)
				var/make = I.create
				while (make > 0)
					new I.item(src.loc)
					make--
				src.working = 0
				src.icon_state = "[state]-idle"
				src.updateUsrDialog()
		return

	attackby(obj/item/weapon/W as obj, mob/user as mob)
		var/load = 0
		if(istype(W, /obj/item/weapon/reagent_containers))
			if (!W:manuaccept) user << "\red This type of food is not usable as an ingredient."
			else
				for(var/mob/O in viewers(user, null)) O.show_message(text("\blue [] loads [] into the [].", user, W, src), 1)
				load = 1
		else if (istype(W, /obj/item/weapon/storage/satchel/hydro))
			for(var/mob/V in viewers(user, null)) V.show_message(text("\blue [] uses the []'s automatic food loader on []!", user, src, W), 1)
			var/amtload = 0
			for (var/obj/item/weapon/reagent_containers/food/M in W.contents)
//				if (!M.manuaccept) continue
				M.loc = src
				amtload++
			if (amtload) user << "\blue [amtload] pieces of food loaded from [W]!"
			else user << "\red No ore loaded!"
		else if(istype(W, /obj/item/weapon/card/emag))
			src.hacked = 1
			user << "\blue You remove the [src]'s product locks!"
		else ..()

		if (load == 1)
			user.u_equip(W)
			W.loc = src
			if ((user.client && user.s_active != src))
				user.client.screen -= W
			W.dropped()
		else if (load == 2)
			user.u_equip(W)
			W.dropped()
			if ((user.client && user.s_active != src))
				user.client.screen -= W
			del W

		src.updateUsrDialog()

	MouseDrop_T(atom/movable/O as mob|obj, mob/user as mob)
		if (istype(O, /obj/structure/closet/))
			for(var/mob/V in viewers(user, null)) V.show_message(text("\blue [] uses the []'s automatic food loader on []!", user, src, O), 1)
			var/amtload = 0
			for (var/obj/item/weapon/reagent_containers/food/M in O.contents)
//				if (!M.manuaccept) continue
				M.loc = src
				amtload++
			if (amtload) user << "\blue [amtload] pieces of food loaded from [O]!"
			else user << "\red No food loaded!"
		else if (istype(O, /obj/item/weapon/ore/))
			for(var/mob/V in viewers(user, null)) V.show_message(text("\blue [] begins quickly stuffing ore into []!", user, src), 1)
			var/staystill = user.loc
			for(var/obj/item/weapon/reagent_containers/food/M in view(1,user))
//				if (!M.manuaccept) continue
				M.loc = src
				sleep(3)
				if (user.loc != staystill) break
			user << "\blue You finish stuffing ore into [src]!"
		/*else if (istype(O, /obj/item/weapon/plant/wheat/metal))
			for(var/mob/V in viewers(user, null)) V.show_message(text("\blue [] begins quickly stuffing [O] into []!", user, src), 1)
			var/staystill = user.loc
			for(var/obj/item/weapon/plant/wheat/metal/M in view(1,user))
				new /obj/item/weapon/ore/mauxite(src)
				del M
				sleep(3)
				if (user.loc != staystill) break
			user << "\blue You finish stuffing [O] into [src]!"*/
		else ..()
		src.updateUsrDialog()

///////////Curdberry Nights Club food////////////////////////////////

/obj/machinery/foodicator/nightclub
	name = "Curdberry Nights Oven"
	desc = "Stamped with the Curdberry logo, this oven makes posh food and nightclub food. Neat."

/datum/cook/posh/plumppie
	name = "Plump Pie"
	item = /obj/item/weapon/reagent_containers/food/snacks/plump_pie
	cost1 = /obj/item/weapon/reagent_containers/food/snacks/mushroom
	cost2 = /obj/item/weapon/reagent_containers/food/snacks/ingredient/flour
	cname1 = "Mushroom"
	cname2 = "Wheat"
	amount1 = 3
	amount2 = 1
	create = 1
	time = 6

/datum/cook/posh/plumpbiscuit
	name = "Plump Biscuit"
	item = /obj/item/weapon/reagent_containers/food/snacks/plumphelmetbiscuit
	cost1 = /obj/item/weapon/reagent_containers/food/snacks/mushroom
	cost2 = /obj/item/weapon/reagent_containers/food/snacks/ingredient/flour
	cname1 = "Mushroom"
	cname2 = "Wheat"
	amount1 = 5
	amount2 = 2
	create = 1
	time = 8

/datum/cook/posh/baguette
	name = "Baguette"
	item = /obj/item/weapon/reagent_containers/food/snacks/baguette
	cost1 = /obj/item/weapon/reagent_containers/food/snacks/ingredient/flour
	cname1 = "Wheat"
	amount1 = 3
	create = 1
	time = 5

/datum/cook/posh/chawanmushi
	name = "Chawanmushi"
	item = /obj/item/weapon/reagent_containers/food/snacks/chawanmushi
	cost1 = /obj/item/weapon/reagent_containers/food/snacks/mushroom
	cost2 = /obj/item/weapon/reagent_containers/food/snacks/ingredient/flour
	cost3 = /obj/item/weapon/reagent_containers/food/snacks/ingredient/meat
	cname1 = "Mushroom"
	cname2 = "Wheat"
	cname3 = "Meat"
	amount1 = 4
	amount2 = 2
	amount3 = 2
	create = 1
	time = 4